﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using Hartley.ApplicationLogic;
using Hartley.DTOs;

namespace WebApplication1
{
    public partial class viewSubCommitteeById : System.Web.UI.Page
    {
        MemberAL memberAL;
        string queryString;
        Guid subCommitteeId;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                queryString = Request.QueryString.ToString();
                subCommitteeId = Guid.Empty;

                bool convertableToGuid = Guid.TryParse(queryString, out subCommitteeId);
                if (convertableToGuid)
                {
                    CreateDynamicTable(subCommitteeId);

                }
                else
                {
                    //Wrong Committee Id
                }

            }
        }


        private void CreateDynamicTable(Guid subCommitteeId)
        {
            PlaceHolder6.Controls.Clear();


            memberAL = new MemberAL();


            List<SubCommitteeDTO> subCommitteeDTO = memberAL.GetSubCommitteeById(subCommitteeId);
            List<SubCommitteeMemberDTO> subCommitteeMemberDTO = memberAL.GetAllSubCommitteeMemberBySubCommitteeId(subCommitteeId).OrderBy(a => a.Role).ToList();

            // Create a Table and set its properties 
            Table tbl = new Table();
            tbl.Width = new Unit("100%");
            tbl.BorderStyle = BorderStyle.Solid;
            // Add the table to the placeholder control
            PlaceHolder6.Controls.Add(tbl);


            Table subtbl = new Table();
            subtbl.Width = new Unit("50%");
            subtbl.BorderStyle = BorderStyle.Solid;

            Label lblSubCommitteeName = new Label();
            lblSubCommitteeName.Text = "Sub Committee Name";

            Label lblExCommittee = new Label();
            lblExCommittee.Text = "Executive Committee";

            Label lblSubCommitteeDesc = new Label();
            lblSubCommitteeDesc.Text = "Description";

            Label lblCommitteeResponsiblity = new Label();
            lblCommitteeResponsiblity.Text = "Responsibility";

            //Label lblCommitteeContribution = new Label();
            //lblCommitteeContribution.Text = "Contribution";


            List<Label> allHeaderLabels = new List<Label>();

            allHeaderLabels.Add(lblSubCommitteeName);
            allHeaderLabels.Add(lblExCommittee);
            allHeaderLabels.Add(lblSubCommitteeDesc);
            allHeaderLabels.Add(lblCommitteeResponsiblity);
            // allHeaderLabels.Add(lblCommitteeContribution);

            int i = 0;
            foreach (Label lblHeader in allHeaderLabels)
            {


                TableRow trHeader = new TableRow();
                trHeader.Width = new Unit("50%");

                TableCell tcHeader = new TableCell();
                TableCell tcHeaderValue = new TableCell();

                HyperLink hlValue = new HyperLink();
                if (i % 2 == 0)
                {
                    tcHeader.BackColor = System.Drawing.Color.Blue;
                    tcHeader.ForeColor = System.Drawing.Color.White;

                    tcHeaderValue.BackColor = System.Drawing.Color.Blue;
                    tcHeaderValue.ForeColor = System.Drawing.Color.White;

                }
                else
                {
                    tcHeader.BackColor = System.Drawing.Color.White;
                    tcHeader.ForeColor = System.Drawing.Color.Blue;

                    tcHeaderValue.BackColor = System.Drawing.Color.White;
                    tcHeaderValue.ForeColor = System.Drawing.Color.Blue;
                }

                if (lblHeader.Text == "Sub Committee Name")
                {
                    hlValue.Text = subCommitteeDTO[0].SubCommitteeName;
                }

                else if (lblHeader.Text == "Executive Committee")
                {
                    hlValue.Text = memberAL.GetCommitteeById( new Guid (subCommitteeDTO[0].MainCommitteeId.ToString()))[0].Title;
                    hlValue.NavigateUrl = "viewExecutiveCommitteeById.aspx?"+ subCommitteeDTO[0].MainCommitteeId;
                }

                else if (lblHeader.Text == "Description")
                {
                    hlValue.Text = subCommitteeDTO[0].Description;
                }
                else if (lblHeader.Text == "Responsibility")
                {
                    hlValue.Text = subCommitteeDTO[0].Responsibility;
                }
                /* else if (lblHeader.Text == "Contribution")
                {
                    lblValue.Text = committeeDTO[0].Contribution;
                }*/
                tcHeader.Controls.Add(lblHeader);
                tcHeaderValue.Controls.Add(hlValue);
                trHeader.Cells.Add(tcHeader);
                trHeader.Cells.Add(tcHeaderValue);

               


                
                tbl.Rows.Add(trHeader);
                i++;
            }

            //to make sure the continuty of the color chhange like blue & white in order j s being increased froom i 
            int j = i;
            
            foreach (SubCommitteeMemberDTO subCommMember in subCommitteeMemberDTO)
            {
                TableRow trSubComMembr = new TableRow();
                trSubComMembr.Width = new Unit("50%");

                TableCell tcSubComMembr = new TableCell();
                TableCell tcSubComMembrName = new TableCell();

                Label lblSubCommitteeMemberRole = new Label();
                HyperLink hlSubComMembrName = new HyperLink();
                if (j % 2 == 0)
                {
                    tcSubComMembr.BackColor = System.Drawing.Color.Blue;
                    tcSubComMembr.ForeColor = System.Drawing.Color.White;

                    tcSubComMembrName.BackColor = System.Drawing.Color.Blue;
                    tcSubComMembrName.ForeColor = System.Drawing.Color.White;

                }
                else
                {
                    tcSubComMembr.BackColor = System.Drawing.Color.White;
                    tcSubComMembr.ForeColor = System.Drawing.Color.Yellow;

                    tcSubComMembrName.BackColor = System.Drawing.Color.White;
                    tcSubComMembrName.ForeColor = System.Drawing.Color.Yellow;
                }
                //add the label in the midddle of left hand cell
              if ((j - i) == subCommitteeMemberDTO.Count / 2)
                {
                    lblSubCommitteeMemberRole.Text = subCommitteeMemberDTO[j-i].Role + "(s)";
               }
                else
                {
                   // lblSubCommitteeMemberRole.Text = "";
                }
                hlSubComMembrName.Text = memberAL.GetMemberById(subCommitteeMemberDTO[j-i].MemberId).SingleOrDefault().FullName;
                hlSubComMembrName.NavigateUrl = "viewMember.aspx?" + subCommitteeMemberDTO[j - i].MemberId;

                tcSubComMembr.Controls.Add(lblSubCommitteeMemberRole);
                tcSubComMembrName.Controls.Add(hlSubComMembrName);
                trSubComMembr.Cells.Add(tcSubComMembr);
                trSubComMembr.Cells.Add(tcSubComMembrName);



                tbl.Rows.Add(trSubComMembr);
                j++;
            }

           

           

            ViewState["dynamictable"] = true;
          
        }
    }
}